(function(factory) {
	var golbal = window;
	if (typeof define === "function" && define.amd) {
		define([], factory(golbal));
	} else {
		factory(golbal)
	}
})(function(golbal) {
	var 
		//定义状态的一个数组
		stateList = ["pending", "fulfilled", "rejected", "processing"],
		noop = function() {},
		//一个promise对象
		Promise = function() {
			
			return {
				thens : [],
				then: function(onSuccess, onFail) {
					var len = arguments.length;
					if (len === 0) {
						throw new Error("需要成功的回调函数");
					}
					this.success = arguments[0];
					this.fail = arguments[1] || noop;
					this.progress = arguments[2] || noop;
					var _dfd = new Deferred() 
					mix(_dfd,this,true);
					this.thens.push(_dfd);
					return this;
				},
				state: state = stateList[0],
				promise: function() {
					return this._promise || this;
				}
			}
		},
		callbacks = {
			success: noop,
			fail: noop,
			progress: noop
		};
	var Deferred = function(mixin) {
		var stateIndex = 0,
			isConfirm = false,
			dfd = {
				resolve: function() {
					isConfirm = 1;
					this.fire(1, arguments);
				},
				reject: function() {
					isConfirm = 2;
					this.fire(2, arguments);
				},
				notify: function() {
					this.fire(3, arguments);
				},
				_promise: new Promise(),
				fire: function(index, args) {
					
					var that = this.thens.shift();
					
					that.state = stateList[index];
					
					if (isConfirm && isConfirm !== index) {
						throw new Error("状态已改变,无法从 " + that.state + " 转为 " + stateList[index]);
					}

					var cbks = Deferred.isPromise(that) ? that._promise : that;

					var Methods = [cbks.success, cbks.fail, cbks.progress];
					
					that.state = stateList[index];
					
					Methods[index - 1].apply(that, args);


					if(this.thens.length !== 0){
						arguments.callee.apply(that,arguments);
					}
				}
			};



		mix(dfd._promise, callbacks, true);
		mix(dfd, dfd._promise, true);
		if (typeof mixin === "function") {
			var ret = mixin(dfd);
			if (ret) {
				return ret;
			}
		} else if (mixin && typeof mixin === "object") {
			mix(dfd._promise, mixin);
			mix(dfd, dfd._promise, true);
		}


		return dfd;
	};
	
	Deferred.all = function(){
		return multiFire(false,arguments);
	};
	Deferred.any = function(){
		return multiFire(true,arguments);	
	};
	Deferred.isPromise = function(obj) {
		return !(obj && obj._promise);
	}
	/*
	* @param any 是否需要完全resolve
	* @param promises 参数数组
	*
	*/
	function multiFire(any,promises){
		var deferred = Deferred(), n = 0, result = [], end
        function loop(promise, index) {
            promise.then(function(ret) {
                if (!end) {
                    result[index] = ret//保证回调的顺序
                    n++;
                    if (any || n >= promises.length) {
                    	console.log(deferred)
                        deferred.resolve(any ? ret : result);
                        end = true
                    }
                }
            }, function(e) {
                end = true
                deferred.reject(e);
            })
        }
        for (var i = 0, l = promises.length; i < l; i++) {
            loop(promises[i], i)
        }
        
        return deferred.promise();
	}

	function mix(target, extra, deep) {
		var deep = arguments[2] || false;
		for (var k in extra) {
			if (deep) {
				target[k] = extra[k];
			}
		}
	}

	return golbal.Deferred = Deferred;
})